<?php
/**
 * 跃飞科技版权所有 @2017
 */

namespace app\admin\controller;

use think\Db;

class Index extends Admin
{
    public function index()
    {
        return $this->fetch();
    }

    public function main()
    {
        $sql = 'select VERSION() as mysql_version';
        $sql = Db::query($sql);
        $mysql_version = $sql[0]['mysql_version'];
        $this->assign('mysql_version',$mysql_version);
        return $this->fetch();
    }

    public function menu()
    {
        return $this->fetch();
    }

    public function top()
    {
        return $this->fetch();
    }

    public function test1()
    {
        phpinfo();
//        pConst();
    }

    /**
     * 清空缓存
     */
    public function clearCache()
    {
        $this->clearPic(TEMP_PATH);
        $this->clearPic(CACHE_PATH);
        $this->success('清理缓存成功',url('admin/index/main'));
    }

    public function clearTmpImg()
    {
        $this->clearPic(ROOT_PATH.'public/assets/upload/tmp');
        $this->success('清理服务器上传的临时缓存图片成功',url('admin/index/main'));
    }


    /**
     * 清除指定目录的所有文件
     * @param  [string] $dir 要清除的目录名
     * @return  void   没有返回值
     */
    public function clearPic($dir)
    {
        $dh=opendir($dir);
        while (!!$file = readdir($dh))
        {
            if($file!="." && $file!="..")
            {
                $fullpath=$dir."/".$file;
                if(!is_dir($fullpath))
                {
                    unlink($fullpath);   //删除不是目录的文件，如tmp/20141231142112.JPG
                }
                else
                {
                    $this->clearPic($fullpath);  //递归删除子目录下的文件，$fullpath=tmp/1
                    rmdir($fullpath);  //删除空目录
                }
            }
        }
        closedir($dh);
    }

}
